<template>
  <div class="search-page">
    <div class="search-header">
      <div class="search-container">
        <el-input
          v-model="searchQuery"
          placeholder="搜索你感兴趣的内容"
          prefix-icon="el-icon-search"
          clearable
          @keyup.enter="handleSearch"
        >
          <template #append>
            <el-button @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
      </div>
    </div>

    <div class="search-content">
      <div class="search-filters">
        <el-select v-model="sortBy" placeholder="排序方式" class="sort-select">
          <el-option
            v-for="item in sortOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>

      <div class="search-results" v-if="searchResults.length > 0">
        <div class="results-grid">
          <VideoCard
            v-for="video in searchResults"
            :key="video.id"
            :video="video"
            class="video-card"
          />
        </div>
      </div>

      <div class="no-results" v-else>
        <el-empty description="暂无搜索结果" />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import VideoCard from '@/components/VideoCard.vue'

export default defineComponent({
  name: 'Search',
  components: {
    VideoCard
  },
  setup() {
    const route = useRoute()
    const searchQuery = ref('')
    const sortBy = ref('newest')
    const searchResults = ref([])

    const sortOptions = [
      { value: 'newest', label: '最新上传' },
      { value: 'hottest', label: '最多播放' },
      { value: 'rating', label: '最高评分' }
    ]

    const handleSearch = () => {
      // 实现搜索逻辑
      console.log('Searching for:', searchQuery.value)
    }

    onMounted(() => {
      const query = route.query.q as string
      if (query) {
        searchQuery.value = query
        handleSearch()
      }
    })

    return {
      searchQuery,
      sortBy,
      sortOptions,
      searchResults,
      handleSearch
    }
  }
})
</script>

<style scoped>
.search-page {
  min-height: 100vh;
  background-color: #f5f5f7;
}

.search-header {
  background: white;
  padding: 2rem 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.search-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 2rem;
}

.search-content {
  max-width: 1400px;
  margin: 2rem auto;
  padding: 0 2rem;
}

.search-filters {
  margin-bottom: 2rem;
}

.sort-select {
  width: 200px;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
}

.no-results {
  text-align: center;
  padding: 4rem 0;
}

@media (max-width: 768px) {
  .search-container {
    padding: 0 1rem;
  }

  .search-content {
    padding: 0 1rem;
  }

  .results-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}
</style> 